package com.jiangore.dao;

import com.jiangore.entity.Member;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.Modifying;
import org.springframework.data.jpa.repository.Query;
import org.springframework.data.repository.query.Param;

/**
 * @Todo
 * @Author jiangtao
 * @CreateBy 2020-03-08 10:04 PM
 * @UpdateBy jiangtao
 * @Version v1.0
 */
public interface IMemberDao extends JpaRepository<Member, Integer> {

    /**
     * 原生sql
     * 占位符传值形式
     * @param account
     * @return
     */
    @Query(nativeQuery = true, value = "select * from a_member where account = ?1")
    Member findByAccount(String account);

    /**
     * 类似于Hibernate HQL
     * // 参数username 映射到数据库字段username
     * @param email
     * @return
     */
    @Query("from Member m where m.email = :email")
    Member findByEmail(@Param("email") String email);

    @Query("from Member m where m.mobile = :mobile")
    Member findByMobile(@Param("mobile") String mobile);

    //保存在 JpaRepository 默认已经支持


    @Query(value = "update a_member set age = ?1 where account = ?4", nativeQuery = true)
    @Modifying
    int updateByAccount(String account, int age);

    /**
     * @param account
     * @return
     */
    @Query(value = "delete from a_member where account = ?1", nativeQuery = true)
    @Modifying
    int deleteByAccount(String account);
}
